home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / SOUND / REPLAYER.SPK / c / replayer < prev   
Text File  |  1998-09-02  |  4KB  |  146 lines

  1.  
  2. /* replayer.c
  3.  
  4.    Replayer -- audio player
  5.    Copyright (c) 1997 Mark Seaborn <mseaborn@argonet.co.uk>
  6.  
  7.    This source file contains miscellaneous functions for Replayer:  the
  8.    functions to create and destroy Replayer objects.
  9. */
  10.  
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. #include "replayer.h"
  15.  
  16.  
  17. /* Initialisation methods for the whole module */
  18.  
  19. #ifndef replayer_NO_ATEXIT_HANDLER
  20.  
  21. /* Used to keep a list of all Replayer file objects. */
  22. typedef struct object_list_node object_list_node;
  23. struct object_list_node {
  24.   replayer_file *obj;
  25.   object_list_node *next;
  26. };
  27.  
  28. static int initialised = 0;
  29. static object_list_node *objects = 0;
  30. static void replayer_initialise(void);
  31. static void replayer_atexit(void);
  32.  
  33. /* To initialise, this registers an atexit handler. */
  34. static void replayer_initialise(void)
  35. {
  36.   if(initialised) return;
  37.   atexit(replayer_atexit);
  38.   initialised = 1;
  39. }
  40.  
  41. /* At exit, this call replayer_sound_tidy() for each Replayer object.  We
  42.    can't delete them, because that isn't our responsibility, and other bits
  43.    of code might try this as well.  This is just an emergency measure. */
  44. static void replayer_atexit(void)
  45. {
  46.   object_list_node *c;
  47.   for(c=objects; c; c=c->next) replayer_sound_tidy(c->obj);
  48. }
  49.  
  50. #endif /* replayer_NO_ATEXIT_HANDLER */
  51.  
  52.  
  53. /* Object initialisation methods */
  54.  
  55. /* Creates a Replay object, given a Replay file.  Returns zero if it
  56.    fails. */
  57. replayer_file *replayer_create_file(const char *filename)
  58. {
  59.   /* Create the object. */
  60.   /* Fail if a null pointer filename is passed. */
  61.   replayer_file *obj;
  62.   if(!filename) return 0;
  63.   obj = malloc(sizeof(replayer_file));
  64.   if(!obj) return 0;
  65.  
  66.   /* Zero necessary fields. */
  67.   obj->header = 0;
  68.   obj->chunks = 0;
  69.   /* Playback preferences. */
  70.   obj->quality = 4; /* Max quality */
  71.   obj->skip_late_data = 0;
  72. #ifndef replayer_NO_BACKWARDS
  73.   obj->backwards = 0;
  74. #endif
  75.   /* Data for sound playing. */
  76.   obj->driver = 0;
  77.   obj->control = 0;
  78.   obj->buffer = 0;
  79.   obj->file = 0;
  80.   obj->playing = 0;
  81.  
  82.   /* Copy the filename we are passed. */
  83.   obj->filename = malloc(strlen(filename) + 1);
  84.   if(!obj->filename) { free(obj); return 0; }
  85.   strcpy(obj->filename, filename);
  86.  
  87.   /* Put this new object on the list of objects. */
  88. #ifndef replayer_NO_ATEXIT_HANDLER
  89.   {
  90.     object_list_node *n = malloc(sizeof(object_list_node));
  91.     if(!n) { free(obj->filename); free(obj); return 0; }
  92.     n->obj = obj;
  93.     n->next = objects;
  94.     objects = n;
  95.     /* Make sure the atexit handler is registered. */
  96.     replayer_initialise();
  97.   }
  98. #endif
  99.  
  100.   /* And return the object. */
  101.   return obj;
  102. }
  103.  
  104. /* Destroys a Replay object, freeing up all resources it used and stopped
  105.    sound playback.  It won't do anything if you pass it a null pointer. */
  106. void replayer_destroy(replayer_file *obj)
  107. {
  108.   if(!obj) return;
  109.  
  110.   /* Remove this object from the list. */
  111. #ifndef replayer_NO_ATEXIT_HANDLER
  112.   {
  113.     object_list_node **c, *old;
  114.     for(c=&objects; *c; c=&(*c)->next) if((*c)->obj == obj) {
  115.       old = *c;
  116.       *c = old->next;
  117.       free(old);
  118.       break;
  119.     }
  120.   }
  121. #endif
  122.  
  123.   /* Tidy up after playback and stop playback if necessary. */
  124.   replayer_sound_tidy(obj);
  125.   /* Free everything, checking for null pointers. */
  126.   free(obj->filename);
  127.  
  128.   /* Free the header, if it was loaded. */
  129.   if(obj->header) {
  130.     int i;
  131.     free(obj->header->title);
  132.     free(obj->header->date_and_copyright);
  133.     free(obj->header->other_details);
  134.     free(obj->header->video_type);
  135.  
  136.     /* Free the soundtrack info. */
  137.     for(i=0; i<obj->header->no_soundtracks; ++i)
  138.       free(obj->header->soundtracks[i].type);
  139.     free(obj->header->soundtracks);
  140.  
  141.     free(obj->header);
  142.   }
  143.   free(obj->chunks);
  144.   free(obj);
  145. }
  146.